package com.ecommerce.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "orders")
@Data
@EntityListeners(AuditingEntityListener.class)
public class Order {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    // 订单号（唯一）
    @Column(unique = true, nullable = false)
    private String orderNumber;
    
    // 用户ID
    @Column(nullable = false)
    private Long userId;
    
    // 订单状态
    @Enumerated(EnumType.STRING)
    private OrderStatus status = OrderStatus.PENDING;
    
    // 订单总金额
    @Column(nullable = false, precision = 10, scale = 2)
    private BigDecimal totalAmount;
    
    // 收货人信息
    @Column(nullable = false)
    private String receiverName;
    
    @Column(nullable = false)
    private String receiverPhone;
    
    @Column(nullable = false)
    private String receiverAddress;
    
    // 支付方式
    @Enumerated(EnumType.STRING)
    private PaymentMethod paymentMethod;
    
    // 支付状态
    @Enumerated(EnumType.STRING)
    private PaymentStatus paymentStatus = PaymentStatus.UNPAID;
    
    // 订单备注
    @Column(columnDefinition = "TEXT")
    private String remark;
    
    // 订单商品列表
    @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonIgnore
    private List<OrderItem> orderItems = new ArrayList<>();
    
    // 创建时间
    @CreatedDate
    private LocalDateTime createdAt;
    
    // 更新时间
    @LastModifiedDate
    private LocalDateTime updatedAt;
    
    // 订单状态枚举
    public enum OrderStatus {
        PENDING("待付款"),
        PAID("已付款"),
        SHIPPED("已发货"),
        DELIVERED("已送达"),
        COMPLETED("已完成"),
        CANCELLED("已取消");
        
        private final String description;
        
        OrderStatus(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
    
    // 支付方式枚举
    public enum PaymentMethod {
        ALIPAY("支付宝"),
        WECHAT("微信支付"),
        BANK_CARD("银行卡"),
        CASH("现金");
        
        private final String description;
        
        PaymentMethod(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
    
    // 支付状态枚举
    public enum PaymentStatus {
        UNPAID("未支付"),
        PAID("已支付"),
        REFUNDED("已退款");
        
        private final String description;
        
        PaymentStatus(String description) {
            this.description = description;
        }
        
        public String getDescription() {
            return description;
        }
    }
} 